/*
    This file is part of Epoch <http://code.google.com/p/epoch/>.

    Copyright (C) 2011 Adam Meily <meily.adam@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#include "Revision.h"
#include <QCryptographicHash>
// done

Revision::Revision()
{

}

Revision::Revision(const Revision& other)
        : m_path(other.m_path), m_uuid(other.m_uuid),
          m_revision(other.m_revision), m_size(other.m_size),
          m_lastSeen(other.m_lastSeen), m_share(other.m_share), Model(other)
{
}

Revision::Revision(int id, const QByteArray& uuid, const Share& share,
                   quint64 size, const QDateTime& lastSeen,
                   const QDateTime& backupTime, const QString& path,
                   int revision)
        : Model(id), m_uuid(uuid), m_share(share), m_size(size),
          m_lastSeen(lastSeen), m_backupTime(backupTime), m_path(path),
          m_revision(revision)
{
}


Revision Revision::fromRecord(const Record& record, const Share& share,
                              const QString& path, bool& ok)
{
    quint64 size;
    Revision rev;
    
    if(record["size"].isEmpty())
    {
        ok = false;
        return rev;
    }
    
    size = record["size"].toULongLong(&ok, 10);
    if(!ok)
        return rev;
    
    //rev.m_backup = backup;
    rev.m_lastSeen = QDateTime::currentDateTime();
    rev.m_path = path;
    rev.m_revision = 1;
    rev.m_share = share;
    rev.m_size = size;
    rev.m_uuid = Revision::clacUuid(share, path);
    
    return rev;
}



Revision::~Revision()
{

}

Revision& Revision::operator=(const Revision& other)
{
    //m_backup = other.m_backup;
    m_lastSeen = other.m_lastSeen;
    //m_name = other.m_name;
    m_path = other.m_path;
    m_revision = other.m_revision;
    m_share = other.m_share;
    m_size = other.m_size;
    m_uuid = other.m_uuid;
    Model::operator=(other);
    
    return *this;
}
/*
const Backup& Revision::backup() const
{
    return m_backup;
}
*/
const QDateTime& Revision::lastSeen() const
{
    return m_lastSeen;
}

QString Revision::name() const
{
    int index = m_path.lastIndexOf('/');
    return m_path.right(m_path.length() - index);
}

const QString& Revision::path() const
{
    return m_path;
}

int Revision::reivision() const
{
    return m_revision;
}

const Share& Revision::share() const
{
    return m_share;
}

quint64 Revision::size() const
{
    return m_size;
}

const QByteArray& Revision::uuid() const
{
    return m_uuid;
}

const QDateTime& Revision::backupTime() const
{
    return m_backupTime;
}


bool Revision::isNull() const
{
    return m_uuid.isEmpty();
}

Revision Revision::inc() const
{
    Revision next(*this);
    next.m_revision++;
    return next;
}

QByteArray Revision::clacUuid(const Share& share, const QString& path)
{
    return QCryptographicHash::hash(share.uuid().toHex() + ":" + path.toUtf8(),
                                    QCryptographicHash::Sha1);
}

bool Revision::isSameRevision(const Record& record) const
{
    bool ok;
    quint64 size = record["size"].toULongLong(&ok, 10);
    if(!ok || size != m_size)
        return false;
    
    qint64 date = record["last-modified"].toLongLong(&ok, 10);
    if(!ok || QDateTime::fromMSecsSinceEpoch(date) > m_backupTime)
        return false;
    
    return true;
}

void Revision::setBackupTime()
{
    m_backupTime = QDateTime::currentDateTime();
    dirty();
}

void Revision::updateLastSeen()
{
    m_lastSeen = QDateTime::currentDateTime();
    dirty();
}

const Revision& Revision::basis() const
{
    return m_basis;
}

void Revision::inject(const Share& share)
{
    m_share = share;
}

void Revision::inject(const Revision& basis)
{
    m_basis = basis;
}


